#include "stdafx.h"
#include "Sprite.h"
#include "Render.h"

namespace a2d
{
//------------------------------------------------------------------

	bool Sprite::Create(const char *FileName)
	{
		if ( !m_tex.Create(FileName) )
			return false;

		SetSize((float)m_tex.GetWidth(), (float)m_tex.GetHeight());
		SetRect(0.0f, 0.0f, (float)m_tex.GetWidth(), (float)m_tex.GetHeight());

		m_flip.x = false;
		m_flip.y = false;

		return true;
	}

	void Sprite::SetSize(float w, float h)
	{
		m_size.x = w;
		m_size.y = h;
	}

	void Sprite::Draw(float x, float y)
	{
		m_tex.Bind();

		Render::Get().GetDefQuad().SetSize(glm::vec2((float)m_tex.GetWidth(), (float)m_tex.GetHeight()));
		Render::Get().GetDefQuad().SetTexRect(glm::vec4(m_rect.x, m_rect.y,
			m_rect.z + m_rect.x - 1, m_rect.w + m_rect.y - 1));

		Render::Get().GetDefQuad().SetSize(m_size);
		Render::Get().GetDefQuad().SetFlip(m_flip.x, m_flip.y);
		Render::Get().GetDefQuad().Draw(glm::vec2(x, y));
	}

	void Sprite::Draw(float x, float y, float z)
	{
		Render::Get().GetDefQuad().SetDepth(z);
		Draw(x, y);
	}

	void Sprite::SetRect(float x1, float y1, float w, float h)
	{	
		m_rect.x = x1;
		m_rect.y = y1;
		m_rect.z = w;
		m_rect.w = h;
		SetSize(w, h);
	}

	void Sprite::SetFlip(bool hor, bool ver)
	{
		m_flip.x = hor;
		m_flip.y = ver;
	}

	void Sprite::Destroy()
	{
		m_tex.Destroy();
	}

//------------------------------------------------------------------
} // namespace a2d
